package uk.org.kano.wifilogin.data;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Hashtable;

public class WifiConfigDef implements Serializable
{
	/**
	 * Object version
	 */
	private static final long serialVersionUID = 20101107L;

	// static objects
	private static MessageDigest md = null;
	static {
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
		}
	}
	
	private static synchronized byte[] doDigest(final String in)
	{
		md.reset();
		return md.digest(in.getBytes());
	}
	
	// The config variables
	private long id = -1;
	private String name = null;
	private String bssid = null;
	private String ssid = null;
	private boolean hidden = false;
	private Config encConfig = null;
	private Hashtable<String, String> props = new Hashtable<String, String>();
	private int allowedAuthAlgorithms = 0;
	private int allowedGroupCiphers = 0;
	private int allowedKeyManagement = 0;
	private int allowedPairwiseCiphers = 0;
	private int allowedProtocols = 0;
	
	public long getId() {
		return id;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		int i;
		long id;
		final byte[] d = doDigest(name);
		for(id=i=0; i<8; i++) {
			id <<= 8;
			id |= d[i];
		}
		this.id = id;
		this.name = name;
	}
	
	public String getBssid() {
		return bssid;
	}
	public void setBssid(String bssid) {
		this.bssid = bssid;
	}
	
	public String getSsid() {
		return ssid;
	}
	public void setSsid(String ssid) {
		this.ssid = ssid;
	}
	
	public boolean isHidden() {
		return hidden;
	}
	public void setHidden(String hidden) {
		this.hidden = Boolean.parseBoolean(hidden);
	}
	
	public Config getAPConfiguration() {
		return encConfig;
	}
	public void setAPConfiguration(Config encConfig) {
		this.encConfig = encConfig;
	}
	
	public Hashtable<String, String> getProperties() {
		return props;
	}
	public String getProperty(String key) {
		return props.get(key);
	}
	public void addProperty(String key, String value) {
		props.put(key, value);
	}
	
	public int getAllowedAuthAlgorithms() {
		return allowedAuthAlgorithms;
	}
	public void setAllowedAuthAlgorithms(String allowedAuthAlgorithms) {
		this.allowedAuthAlgorithms = Integer.parseInt(allowedAuthAlgorithms, 16);
	}
	
	public int getAllowedGroupCiphers() {
		return allowedGroupCiphers;
	}
	public void setAllowedGroupCiphers(String allowedGroupCiphers) {
		this.allowedGroupCiphers = Integer.parseInt(allowedGroupCiphers, 16);
	}
	
	public int getAllowedKeyManagement() {
		return allowedKeyManagement;
	}
	public void setAllowedKeyManagement(String allowedKeyManagement) {
		this.allowedKeyManagement = Integer.parseInt(allowedKeyManagement, 16);
	}
	
	public int getAllowedPairwiseCiphers() {
		return allowedPairwiseCiphers;
	}
	public void setAllowedPairwiseCiphers(String allowedPairwiseCiphers) {
		this.allowedPairwiseCiphers = Integer.parseInt(allowedPairwiseCiphers, 16);
	}
	
	public int getAllowedProtocols() {
		return allowedProtocols;
	}
	public void setAllowedProtocols(String allowedProtocols) {
		this.allowedProtocols = Integer.parseInt(allowedProtocols, 16);
	}


	/**
	 * Base class for the encryption configurations to derive from
	 *
	 */
	private abstract class Config {
	}
	
	/**
	 * An open accesspoint configuration
	 *
	 */
	public class OpenConfig extends Config {
	}
	
	/**
	 * A WEP accesspoint configuration
	 *
	 */
	public class WepConfig extends Config {
		private String[] keys = new String[4];
		private int last = 0;

		public String[] getKeys() {
			return keys;
		}

		public void setKey(String key, int index) {
			if (index < keys.length)
				keys[index] = key;
		}
		
		public void addKey(String key) {
			if (last < keys.length)
				setKey(key, last++);
		}
	}
	
	/**
	 * A WPA PSK configuration
	 *
	 */
	public class WpaPskConfig extends Config {
		private String psk = null;

		public String getPSK() {
			return psk;
		}

		public void setPSK(String psk) {
			this.psk = psk;
		}
	}
}
